package com.bnuz.vtl.service;

import com.bnuz.vtl.model.PrivateNode;
import com.bnuz.vtl.model.PublicNode;
import com.bnuz.vtl.model.Subject;
import com.bnuz.vtl.model.User;
import com.bnuz.vtl.repo.PublicNodeRepo;
import com.bnuz.vtl.repo.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements BaseService<User, String> {

    @Autowired
    private UserRepo userRepo;

    @Autowired
    private PublicNodeRepo publicNodeRepo;

    @Override
    public User save(User user) {
        return userRepo.save(user);
    }

    @Override
    public void deleteById(String userId) {
        userRepo.deleteById(userId);
    }

    @Override
    public List<User> findAll() {
        return userRepo.findAll();
    }

    @Override
    public User findById(String userId) {
        return userRepo.findById(userId).orElse(null);
    }

    public User findByUsername(String username) { return userRepo.findByUsername(username).orElse(null); }

    public void followSubject(String username, String subjectId){
        userRepo.followSubject(username, subjectId);
    }

    public PublicNode getUserSubjectTree(String username) {
        PublicNode node = userRepo.getUserSubjectTree(username);
        return publicNodeRepo.findById(node.getPublicNodeId(), -1).orElse(null);
    }

    public List<PrivateNode> getUserNodes(String username) {
        return userRepo.getUserNodes(username);
    }
}
